package com.jamcodingnm.apps.abqevents.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.jamcodingnm.apps.abqevents.R;


/**
 * Main Activity for ABQ RSS android app.
 * <p/>
 * 
 * @author jeremy
 * 
 */
public class MainActivity extends BaseActivity implements
		ActionBar.TabListener, ViewPager.OnPageChangeListener {

	/** Tag for logging debug messages. */
	private static final String TAG = MainActivity.class.getName();

	ViewPager mViewPager;

	FeedFragment mFeedFragment;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		if (isFinishing()) {
			return;
		}

		setContentView(R.layout.main_pager);

		mViewPager = (ViewPager) findViewById(R.id.pager);
		FragmentManager fm = getSupportFragmentManager();

		if (mViewPager != null) {
			mViewPager.setAdapter(new HomePagerAdapter(
					getSupportFragmentManager()));
			mViewPager.setOnPageChangeListener(this);

			final ActionBar actionBar = getSupportActionBar();
			actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
			actionBar.addTab(actionBar.newTab().setText("Events")
					.setTabListener(this));
			actionBar.addTab(actionBar.newTab().setText("News")
					.setTabListener(this));
			actionBar.addTab(actionBar.newTab().setText("Volunteerism")
					.setTabListener(this));
		}

		getSupportActionBar().setHomeButtonEnabled(false);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.activity_main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_refresh:
			mFeedFragment.refresh();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	/**
	 * Log a debug message.
	 * <p/>
	 * 
	 * @param msg
	 *            String message to log
	 */
	private void log(String msg) {
		Log.d(TAG, msg);
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		mViewPager.setCurrentItem(tab.getPosition());
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub

	}

	private String getURL(int index) {
		switch (index) {
		case 0:
			return this.getResources().getString(R.string.rss_events_url);
		case 1:
			return this.getResources().getString(R.string.rss_news_url);
		case 2:
			return this.getResources().getString(R.string.rss_volunteerism_url);
		default:
			return this.getResources().getString(R.string.rss_events_url);
		}
	}

	private class HomePagerAdapter extends FragmentPagerAdapter {

		public HomePagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {

			Bundle bundle = new Bundle();
			bundle.putString("RSS_URL", getURL(position));
			mFeedFragment = new FeedFragment();
			mFeedFragment.setArguments(bundle);

			switch (position) {
			case 0:
				return mFeedFragment;
			case 1:
				return mFeedFragment;
			case 2:
				return mFeedFragment;
			default:
				return null;
			}
		}

		public int getCount() {
			return 3;
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	@Override
	public void onPageSelected(int position) {
		getSupportActionBar().setSelectedNavigationItem(position);
	}
}
